#include <Windows.h>
#include "Helix.h"
#include "Timer.h"
#include "HighPerformanceTimer.h"

namespace helix
{


	HighPerformanceTimer::HighPerformanceTimer()
	{
		QueryPerformanceCounter(&lastTickCount);		// Get Tick Count
		QueryPerformanceFrequency(&ticFrequency);       // Get the frequency of the timer
	}

	HighPerformanceTimer::~HighPerformanceTimer()
	{

	}

	long int HighPerformanceTimer::resolution()		//get the ticks per second of the timer
	{
		return this->ticFrequency.LowPart;
	}

	long int HighPerformanceTimer::lastTime()		//get the number of ticks of the timer of the last update
	{
		return this->lastTickCount.LowPart;			
	}
	long int HighPerformanceTimer::thisTime()		//get the current number of ticks of the timer
	{
		LARGE_INTEGER tmp;
		QueryPerformanceCounter(&tmp);
		return tmp.LowPart;
	}

	void HighPerformanceTimer::updateTime()		//update the timer and calculate the time difference between now and the last update
	{
		LARGE_INTEGER tmp;
		long int dTics;
		QueryPerformanceCounter(&tmp);
		if(tmp.HighPart > lastTickCount.HighPart)	//the timer rolled over
			dTics=tmp.LowPart + (HIGH_PERFORMANCE_TIMER_LONG_SIZE - lastTickCount.LowPart);
		else
			dTics=tmp.LowPart - lastTickCount.LowPart;

		lastTickCount=tmp;

		seconds=(double)dTics/(double)(ticFrequency.LowPart);
	}

	double HighPerformanceTimer::dSeconds()		//get the time difference between the last two updates in seconds
	{
		return seconds;
	}

}